﻿#include "widget_replace.h"
#include "ui_widget_replace.h"

widget_replace::widget_replace(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::widget_replace)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_QuitOnClose,false);
    setWindowFlags(Qt::WindowStaysOnTopHint);
    setWindowTitle(tr("替换"));
    setFixedSize(320,150);
    installEventFilter(this);
}

widget_replace::~widget_replace()
{
    delete ui;
}

bool widget_replace::eventFilter(QObject *, QEvent *event)
{
    if(QEvent::WindowDeactivate == event->type()){
        setWindowOpacity(0.5);
        return true ;
    }else if(QEvent::WindowActivate == event->type()){
        setWindowOpacity(1);
        return true;
    }
    return false;
}

void widget_replace::on_pushButton_next_clicked()
{
    QTextDocument::FindFlags opt = 0;
    if(ui->checkBox_caseSensitively->isChecked()){
        opt |= QTextDocument::FindCaseSensitively;
    }
    emit sig_findString(ui->preString->text(), ui->afterString->text(), opt, 0);
}

void widget_replace::on_pushButton_repllace_clicked()
{
    QTextDocument::FindFlags opt = 0;
    if(ui->checkBox_caseSensitively->isChecked()){
        opt |= QTextDocument::FindCaseSensitively;
    }
    emit sig_findString(ui->preString->text(), ui->afterString->text(), opt, 1);
}

void widget_replace::on_pushButton_replace_all_clicked()
{
    QTextDocument::FindFlags opt = 0;
    if(ui->checkBox_caseSensitively->isChecked()){
        opt |= QTextDocument::FindCaseSensitively;
    }
    emit sig_findString(ui->preString->text(), ui->afterString->text(), opt, 2);
}

void widget_replace::on_pushButton_cancel_clicked()
{
    close();
}
